//
//  TrainingGoalItem.swift
//  DaRings
//
//  Created by HFY on 2022/11/30.
//

import UIKit
import MYModule

open class TrainingGoalItemModel: NSObject {
    
    public var contentType: TrainingTypeContent
    
    /// 目标值
    public var target: Double
    
    /// 单位
    public var unit: String
    
    /// 额外说明
    public var desc: String?
    
    /// 是否被选择
    public var isSelected: Bool = false
    
    public var selectedTintColor: UIColor?
    
    public var isEmpty: Bool = false
    
    public static func emptyModel(contentType: TrainingTypeContent) ->TrainingGoalItemModel {
        
       let item = TrainingGoalItemModel(contentType: contentType, target: 0, unit: "")
        item.isEmpty = true
        return item
    }
    

    public init(contentType: TrainingTypeContent, target: Double, unit: String) {
        self.contentType = contentType
        self.target = target
        self.unit = unit
        super.init()
        
    }
    
}

extension TrainingGoalItemModel {
    
    public var targetTitle: String? {
        
        switch contentType {
            
        case .distance:
            
            let distance = Unit.sharedInstance.goalDistance(target).distance/1000.0
            let unitName = self.unit
            
            let distanceStrArr = "\(distance)".components(separatedBy: ".")
            if distanceStrArr.count > 1 && distanceStrArr[1] != "0" {
                return "\(distance) \(unitName)"
            }
            
            return "\(Int(distance)) \(unitName)"
            
        case .time:
            
            return "\(Int(target/60)) \(unit)"
            
        case .pace:
            let pace = Unit.sharedInstance.goalMetricPace(target)
            return String(format: "%02d'00\"", Int(pace))
            
        case .calorie:
            
            return "\(Int(target)) \(unit)"
            
        default:
            return nil
        }
        
    }
    
    public var selectedTargetTitle: (target: String, unit: String)? {
        
        switch contentType {
            
        case .distance:
            
            let tuple = Unit.sharedInstance.goalDistance(target)
            let distance = tuple.distance/1000.0
            return (String(format: "%.2f", distance), unit)
            
        case .time:
            
            let tuple = Unit.sharedInstance.timeOf(seconds: Int(target), granularity: .hour)
            
            return ("\(tuple.hour.leadingZero):\(tuple.minute.leadingZero)", "gps_training_goal_setting_time_current_unit".language)
            
        case .pace:
            let pace = Unit.sharedInstance.goalMetricPace(target)
            return (String(format: "%02d'00\"", Int(pace)), unit)
            
        case .calorie:
            
            return ("\(Int(target))", unit)
            
        default:
            return nil
        }
        
    }
    
    
    
}
